package com.quickcode.example.threadlocal;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@Slf4j
public class ThreadLocalExample1 {

    public static ThreadLocal<String> stringThreadLocal = new ThreadLocal<>();

    public static ThreadLocal<Integer> integerThreadLocal = new ThreadLocal<>();

    public static void main(String[] args) {
        ExecutorService executorService = Executors.newCachedThreadPool();
        for(int i = 0 ; i <= 30 ; i ++ ) {
            final int number = i;
            executorService.execute(() -> {
                stringThreadLocal.set("number:" + number);
                integerThreadLocal.set(number);
                try {
                    Thread.sleep(100l);
                } catch (InterruptedException e) {

                }
                testString(number);
                stringThreadLocal.remove();
                integerThreadLocal.remove();
            });
        }

    }

    public static void testString(int i) {
        log.info("number:{},String:{},integer:{}",i,stringThreadLocal.get(),integerThreadLocal.get());
    }
}